package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.DataCenterService;
import com.yixun.qfbao.apidto.GetVisitStateResDto;
import com.yixun.qfbao.apidto.GetVisitStateReqDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.VisitStateInfo;
import com.yixun.qfbao.utils.CalculateUtil;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.GetVisitStateReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 查询到访数据报表
 *
 * @classname: GetVisitStateListHander.java
 * @author: Beau
 * @create: 2020/01/16 10:03
 * @version: 1.0.0
 **/
@Service("getVisitStateListHander")
public class GetVisitStateListHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(GetVisitStateListHander.class);

    @Autowired
    private LoginUtils loginUtils;

    @Autowired
    private DataCenterService dataCenterService;
    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        GetVisitStateReqVo reqDto1 = (GetVisitStateReqVo) reqDto;
        GetVisitStateReqDto params = reqDto1.getParams();
        BaseResponseDTO<GetVisitStateResDto> res = new BaseResponseDTO<>();
        GetVisitStateResDto resDto = new GetVisitStateResDto();

        PartnerInfo pi = loginUtils.getUser(params.getUserId());
        if (pi == null) {
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("未找到用户信息");
            return res;
        }
        try {
            params.setDevCompanyId(pi.getHigherid());
            GetVisitStateResDto dto = dataCenterService.getVisitStateList(params);

            List<VisitStateInfo> collect = dto.getGetVisitStateList().stream().filter(visitStateInfo -> {
                String signRate = CalculateUtil.signRate(visitStateInfo.getVisitCount() + "", visitStateInfo.getSignCount() + "");
                visitStateInfo.setSignRate(signRate);
                visitStateInfo.setSignRateMax("0");
                return true;
            }).collect(Collectors.toList());
            List<String> signRateList = collect.stream().map(VisitStateInfo::getSignRate).collect(Collectors.toList());
            ArrayList<Double> doubles = new ArrayList<>();
            for (String s : signRateList) {
                Double aLong = Double.parseDouble(s.substring(0, s.length() - 1));
                doubles.add(aLong);
            }
            Double max = Collections.max(doubles);
            double v = max % 1;
            String signRateMax;
            if (v == 0.0) {
                signRateMax = max + "0%";
            } else {
                signRateMax = max + "%";
            }
            if (!"0.00%".equals(signRateMax)) {
                for (VisitStateInfo visitStateInfo : collect) {
                    if (visitStateInfo.getSignRate().equals(signRateMax)) {
                        visitStateInfo.setSignRateMax("1");
                    }
                }
            }

            resDto.setGetVisitStateList(collect);
            resDto.setStartDate(dto.getStartDate());
            resDto.setEndDate(dto.getEndDate());
            res.setResData(resDto);
            res.setResult(ResultEnum.SUCCESS.getNo());
            return res;
        } catch (Exception e) {
            logger.error("GetVisitStatePageHander error: {}", e);
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("查询到访数据报表失败");
            return res;
        }
    }
}
